package impl.repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import dao.AccessDatabase;
import domain.Product;
import domain.ProductApparel;
import interfaces.repository.ProductApparelRepository;

public class StubProductApparelRepositoryImpl implements ProductApparelRepository{

	@Override
	public List<ProductApparel> findProductByID(Integer id) {
		
		ArrayList<ProductApparel> products = new ArrayList<ProductApparel>();
		ProductApparel product;
				
				
				try{

	        	AccessDatabase a = new AccessDatabase();
	            Connection con = a.conectar();
	            Statement st = con.createStatement();
	            ResultSet rs = st.executeQuery("SELECT * FROM productapparel where idProductApparel='"+id+"';");
	            	while(rs.next()){
                
	            		product = new ProductApparel(
                		
                		rs.getInt("idProductApparel"),
                		rs.getString("name"), 
                		rs.getString("description"), 
                		rs.getString("picture"), 
                		rs.getFloat("price")
                		
                		
                ){};
                
                product.setColor(rs.getString("color"));
                product.setSize(rs.getFloat("size"));
                product.setMaterial(rs.getString("material"));
                
               
                
               
                 products.add(product);
                };
                    
                con.close();
                return products ;
            
            } catch (SQLException ex) {
                        Logger.getLogger(ProductApparelRepository.class.getName()).log(Level.SEVERE, null, ex);
        
                    }
		
		return null;
	}

	@Override
	public List<ProductApparel> findProductByName(String name) {

		ArrayList<ProductApparel> products = new ArrayList<ProductApparel>();
		ProductApparel product;
				
				
				try{

	        	AccessDatabase a = new AccessDatabase();
	            Connection con = a.conectar();
	            Statement st = con.createStatement();
	            ResultSet rs = st.executeQuery("SELECT * FROM productapparel where name='"+name+"';");
	            	while(rs.next()){
                
	            		product = new ProductApparel(
                		
                		rs.getInt("idProductApparel"),
                		rs.getString("name"), 
                		rs.getString("description"), 
                		rs.getString("picture"), 
                		rs.getFloat("price")
                		
                		
                ){};
                
                product.setColor(rs.getString("color"));
                product.setSize(rs.getFloat("size"));
                product.setMaterial(rs.getString("material"));
                
               
                
               
                 products.add(product);
                };
                    
                con.close();
                return products ;
            
            } catch (SQLException ex) {
                        Logger.getLogger(ProductApparelRepository.class.getName()).log(Level.SEVERE, null, ex);
        
                    }

		return null;
	}

	@Override
	public boolean add(ProductApparel product) {
		try{
         	AccessDatabase a = new AccessDatabase();
         	Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("INSERT INTO `productapparel` (`name`,`picture`,`price`,`description`,`size`,`color`,`material`) VALUES ('"+product.getName()+"','"+product.getPicture()+"','"+product.getPrice()+"','"+product.getDescription()+"','"+product.getSize()+"','"+product.getColor()+"','"+product.getMaterial()+"');");
            con.close();
                    
            return true;
            }catch(Exception t){
                
            
            return false;
            
              }
	}

	@Override
	public boolean edit(ProductApparel before, ProductApparel after) {

		 try{
			 
          	 AccessDatabase a = new AccessDatabase();
          	 Connection con = a.conectar();
             Statement st = con.createStatement();
             st.execute("UPDATE `productapparel` SET `name`='"+after.getName()+"', `picture`='"+after.getPicture()+"', `price`='"+after.getPrice()+"', `description`='"+after.getDescription()+"', `size`='"+after.getSize()+"', `color`='"+after.getColor()+"', `material`='"+after.getMaterial()+"'  WHERE `idProductApparel`='"+before.getId()+"'");
             con.close();
               
             }catch(Exception t){
                 
             
               
               }
		

		return false;
	}

	@Override
	public boolean delete(Integer id) {
		// TODO Auto-generated method stub
		return false;
	}
	
	

}
